<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="600px"
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      label-position="left"
      label-width="120px"
      :model="form"
      :rules="rules"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item
            label="标准编码"
            label-position="left"
            prop="standardCode"
          >
            <el-input v-model.trim="form.standardCode" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="标准名称" prop="standardName">
            <el-input v-model.trim="form.standardName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model.trim="form.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
  import { ref } from 'vue'
  import { addStandardDocApi, updStandardDocApi } from '@/api/qm/standard_api'
  import { localStorageConfig } from '@/config/localstorage.config'

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const $baseMessage = inject('$baseMessage')
  const dialogVisible = ref(false),
    dialogTitle = ref('增加检验标准'),
    formRef = ref(),
    form = reactive({}),
    rules = reactive({
      standardCode: [
        { required: true, trigger: 'blur', message: '请输入检验标准编码' },
      ],
      standardName: [
        { required: true, trigger: 'blur', message: '请输入检验标准名称' },
      ],
    })

  let baseRecord = ref(null)

  const openModuleDialog = (record) => {
    dialogVisible.value = true
    if (record) {
      dialogTitle.value = '修改检验标准'
      baseRecord.value = record
      form['pkStandardDoc'] = record.pkStandardDoc ? record.pkStandardDoc : ''
      form['standardCode'] = record.standardCode ? record.standardCode : ''
      form['standardName'] = record.standardName ? record.standardName : ''
      form['remark'] = record.remark ? record.remark : ''
    } else {
      dialogTitle.value = '增加检验标准'
      form['pkStandardDoc'] = ''
      form['standardCode'] = ''
      form['standardName'] = ''
      form['remark'] = ''
      form['pkCorp'] = pkCorp
    }
  }
  const save = () => {
    if (!formRef.value) return
    formRef.value.validate(async (valid) => {
      if (valid) {
        let res = null
        if (baseRecord.value) {
          res = await updStandardDocApi({
            ...form,
          })
        } else {
          res = await addStandardDocApi({
            ...form,
          })
        }
        if (res && res.code === 2000) {
          $baseMessage('操作成功', 'success')
          _emit('success-callback')
          closeDialog()
        } else {
          $baseMessage(res && res.msg ? res.msg : '操作失败,请重试', 'error')
        }
      } else {
        return false
      }
    })
  }
  const closeDialog = () => {
    baseRecord.value = null
    dialogVisible.value = false
  }
  defineExpose({ openModuleDialog })
  const _emit = defineEmits(['success-callback'])
</script>
